<template>
  <dialog-box
    :isDialog="false"
    ref="myDialog"
    :showFooter="true"
    width="600px"
    @close="close"
    @sureHandle="sureHandle"
    :isFooterSlot="true"
    :title="state.title"
  >
    <template v-slot:dialogMain>
      <div>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="auto"
          class="demo-ruleForm"
          size="default"
          status-icon
        >
          <el-form-item label="下载任务名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
          <el-form-item label="下载任务说明" prop="desc">
            <el-input v-model="ruleForm.desc" type="textarea" />
          </el-form-item>
          <el-form-item label="取数类型" prop="region">
            <el-select v-model="ruleForm.region">
              <el-option
                v-for="(it, i) in state.fetchTypes"
                :key="i"
                :label="it.key"
                :value="it.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="关联取数任务" prop="region">
            <el-select v-model="ruleForm.region">
              <el-option
                v-for="(it, i) in state.configTypes"
                :key="i"
                :label="it.key"
                :value="it.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </template>
  </dialog-box>
</template>

<script setup lang="ts">
import dialogBox from "@/components/dialog-box/index.vue";
import { onMounted, computed, reactive, toRefs, ref } from "vue";
import type { ComponentSize, FormInstance, FormRules } from "element-plus";
const myDialog = ref(null);

const ruleFormRef = ref<FormInstance>();
    interface RuleForm {
  name: string;
  times: string;
  region: string;
  count: string;
  date2: string;
  delivery: boolean;
  location: string;
  type: string[];
  resource: string;
  desc: string;
}
const ruleForm = reactive<RuleForm>({
  name: "Hello",
  times: "",
  region: "",
  count: "",
  date2: "",
  delivery: false,
  location: "",
  type: [],
  resource: "",
  desc: "",
});

let state = reactive({
  title: "",
  fetchTypes: [
    { key: "周期性取数", value: 1 },
    { key: "立即取数", value: 2 },
  ],
  configTypes: [
    { key: "xx", value: 1 },
    { key: "xxx", value: 2 },
    { key: "xxxx", value: 3 },
  ],
});
// 定义完成后，用defineExpose()暴露给父组件
const open = (title: string) => {
  state.title = title;
  myDialog.value.handleOpen();
};
const close = () => {
  resetForm();
  myDialog.value.handleClose();
};
const sureHandle = () => {
  submitForm();
};
defineExpose({
  open,
  close,
});


const submitForm = async () => {
  if (!ruleFormRef) return;
  console.log(ruleFormRef, "23232");
  await ruleFormRef.value?.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = () => {
  if (!ruleFormRef) return;
  ruleFormRef.value?.resetFields();
};
const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: "Please input Activity name", trigger: "blur" },
    { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" },
  ],
});
</script>

<style scoped>
h2 {
  margin-bottom: 20px;
}
.mb-20 {
  margin-bottom: 20px;
}
.mr-10 {
  margin-right: 10px;
}
.ml-10 {
  margin-left: 10px;
}
.steps-box {
  width: 300px;
  margin: 0 auto 30px;
}
.title-box {
  display: flex;
  align-items: center;
}
.seach-box {
  display: flex;
  padding: 0 10px;
  box-sizing: border-box;
}
.footer-btns {
  width: 100%;
  display: flex;
  justify-content: center;
}
</style>
